package com.kefu.call.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.call.model.ActivityBaseInfo;
import com.kefu.call.service.ActivityBaseInfoService;
import com.kefu.call.vo.ActivityBaseInfoInsertVo;
import com.kefu.call.vo.ActivityBaseInfoSearchVo;
import com.kefu.call.vo.ActivityBaseInfoUpdateVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Api(tags = "活动管理")
@RestController
@RequestMapping("call/activity/base")
public class ActivityBaseInfoController extends AbstractBaseController {

    @Autowired
    private ActivityBaseInfoService activityBaseInfoService;

    @PostMapping("/add")
    @ApiOperation(value = "新增活动管理", notes = "新增活动管理")
    public Result<String> add(@Validated @RequestBody ActivityBaseInfoInsertVo activityBaseInfoVo) {
    	logger.info("新增活动管理:{}", activityBaseInfoVo);

        String result = activityBaseInfoService.addActivity(activityBaseInfoVo);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除活动管理", notes = "删除活动管理")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除活动管理:{}", delete);

        activityBaseInfoService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改活动管理", notes = "修改活动管理,启用得清单不可以直接停用,需要先暂停，在停用，停用得清单才可以删除")
    public Result<String> update(@Validated @RequestBody ActivityBaseInfoUpdateVo activityBaseInfoVo) {
    	logger.info("修改活动管理:{}", activityBaseInfoVo);

        String result = activityBaseInfoService.updateActivity(activityBaseInfoVo);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "活动管理详情", notes = "活动管理详情")
    public Result<ActivityBaseInfo> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("活动管理查看详情:{}", detail);
        
        ActivityBaseInfo activityBaseInfo = activityBaseInfoService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(activityBaseInfo);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询活动管理", notes = "查询活动管理")
    public Result<Page<ActivityBaseInfo>> list(@Validated @RequestBody ActivityBaseInfoSearchVo search) {    
        logger.info("查询活动管理:{}", search);
        if (Objects.nonNull(search.getCreateTimeStart()) && Objects.nonNull(search.getCreateTimeEnd())) {
            if (search.getCreateTimeEnd() < search.getCreateTimeStart()) {
                throw BusinessException.msg("结束时间不能小于开始时间");
            }
        }
        return ResultGenerator.genSuccessResult(activityBaseInfoService.queryActivityList(search));
    }
}
